

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  
    <meta name="description" content="@[toc]参考资料：视频资料 一、Quartz Quartz 官网：scheduler.orgQuartz 是 OpenSymphon 开源组织发布的开源项目，可以和J2EE 和 J2SE 应用程序相结合，也可以单独使用。 Quartz 是开源且具有丰富特性的“任务调度库”，能够集成任何的Java应用，小到独立的应用，大到电子商业系统。Quartz既能创建简单，也能创建复杂的任务调度，以执行上十">
<meta property="og:type" content="article">
<meta property="og:title" content="Quartz 笔记1 任务调度框架 _ 核心组件 Job、Trigger 和 Scheduler _ 入门案例 _ 监听器">
<meta property="og:url" content="http://example.com/2022/07/09/Quartz%20%E7%AC%94%E8%AE%B01%20%E4%BB%BB%E5%8A%A1%E8%B0%83%E5%BA%A6%E6%A1%86%E6%9E%B6%20_%20%E6%A0%B8%E5%BF%83%E7%BB%84%E4%BB%B6%20Job%E3%80%81Trigger%20%E5%92%8C%20Scheduler%20_%20%E5%85%A5%E9%97%A8%E6%A1%88%E4%BE%8B%20_%20%E7%9B%91%E5%90%AC%E5%99%A8/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="@[toc]参考资料：视频资料 一、Quartz Quartz 官网：scheduler.orgQuartz 是 OpenSymphon 开源组织发布的开源项目，可以和J2EE 和 J2SE 应用程序相结合，也可以单独使用。 Quartz 是开源且具有丰富特性的“任务调度库”，能够集成任何的Java应用，小到独立的应用，大到电子商业系统。Quartz既能创建简单，也能创建复杂的任务调度，以执行上十">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/4552eecfd2564f3298af89316a702091.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/4eaa4becc5e6498ba781d6ab88794e7e.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/dc76590d539d4640965b6e32412b1522.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/a8faf8e530a84b7ab3affed81791c635.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/4769f8666beb4f67ab6929fb5f54a569.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/af1994814305405e8853a1c9e9ad67b4.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/873b003339264042a444dea87d895414.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/ac3983fbf23f494680d3d13e21fa5d2a.png">
<meta property="article:published_time" content="2022-07-09T09:29:08.000Z">
<meta property="article:modified_time" content="2022-08-22T15:47:43.874Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="Quartz">
<meta property="article:tag" content="任务调度">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/4552eecfd2564f3298af89316a702091.png">
  
  
  
  <title>Quartz 笔记1 任务调度框架 _ 核心组件 Job、Trigger 和 Scheduler _ 入门案例 _ 监听器 - Hexo</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>尤 Ni&#39;s Blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="Quartz 笔记1 任务调度框架 _ 核心组件 Job、Trigger 和 Scheduler _ 入门案例 _ 监听器"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        John Doe
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-07-09 17:29" pubdate>
          2022年7月9日 下午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          27k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          224 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Quartz 笔记1 任务调度框架 _ 核心组件 Job、Trigger 和 Scheduler _ 入门案例 _ 监听器</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：1 小时前
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <p>@[toc]<br>参考资料：<a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV19t41127de">视频资料</a></p>
<h1 id="一、Quartz"><a href="#一、Quartz" class="headerlink" title="一、Quartz"></a>一、Quartz</h1><hr>
<p>Quartz 官网：<a target="_blank" rel="noopener" href="https://venture.com/domains/scheduler.org">scheduler.org</a><br>Quartz 是 OpenSymphon 开源组织发布的开源项目，可以和J2EE 和 J2SE 应用程序相结合，也可以单独使用。</p>
<p>Quartz 是开源且具有丰富特性的“任务调度库”，能够集成任何的Java应用，小到独立的应用，大到电子商业系统。Quartz既能创建简单，也能创建复杂的任务调度，以执行上十、上百，甚至上万的任务。任务 job 被定义为标准的Java组件，能够执行任何想要实现的功能。<br>Quartz 调度框架包含许多企业级的任务如 JTA 事务、 集群的支持。</p>
<p>简而言之，Quartz 就是基于Java实现的任务调度框架，用于执行自定义的任务。</p>
<h1 id="二、Quartz-运行环境"><a href="#二、Quartz-运行环境" class="headerlink" title="二、Quartz 运行环境"></a>二、Quartz 运行环境</h1><hr>
<ul>
<li>Quartz 可以运行嵌入在另一个独立式应用程序</li>
<li>Quartz 可以在应用程序服务器（或Servlet容器）内被实例化，并且参与事务</li>
<li>Quartz 可以作为一个独立的程序运行（其自己的Java虚拟机内），可以通过RMI使用</li>
<li>Quartz 可以被实例化，作为独立的项目集群（负载均衡和故障转移功能），用于作业的执行</li>
</ul>
<h1 id="三、Quartz-核心组件"><a href="#三、Quartz-核心组件" class="headerlink" title="三、Quartz 核心组件"></a>三、Quartz 核心组件</h1><hr>
<ul>
<li>任务 Job<ul>
<li>Job 就是想要实现的任务类，每一个 Job 必须实现 org.quartz.job接口， 且只需要实现接口定义的 execute()方法</li>
</ul>
</li>
<li>触发器 Trigger<ul>
<li>Trigger 为你执行任务的触发器，，比如每天定时3点发送一份统计邮件， Trigger将会设置3点进行执行该任务。</li>
<li>Trigger主要包含两种 SimpleTrigger 和 CronTrigger 两种</li>
</ul>
</li>
<li>调度器 Scheduler<ul>
<li>Scheduler 为任务的调度器，它会将任务 Job 以及触发器 Trigger 整合起来，负责基于 Trigger 设定的时间来执行Job</li>
</ul>
</li>
</ul>
<h1 id="四、Quartz-设计模式"><a href="#四、Quartz-设计模式" class="headerlink" title="四、Quartz 设计模式"></a>四、Quartz 设计模式</h1><hr>
<ul>
<li><p>Builder 模式<br>官方案例中使用 newJob(字节码).withXXX.build()的方式创建工作<br><img src="https://img-blog.csdnimg.cn/4552eecfd2564f3298af89316a702091.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
</li>
<li><p>Factory 模式<br>官方案例中通过 StdSchedulerFactory类获取 调度器对象<br><img src="https://img-blog.csdnimg.cn/4eaa4becc5e6498ba781d6ab88794e7e.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
</li>
<li><p>组件模式<br>Job、Trigger触发器 和 Scheduler调度器三个组件<br><img src="https://img-blog.csdnimg.cn/dc76590d539d4640965b6e32412b1522.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
</li>
<li><p>链式编程<br>Quartz的类其中部分方法返回的结果就是当前对象实例，所以可以支持链式编程<br><img src="https://img-blog.csdnimg.cn/a8faf8e530a84b7ab3affed81791c635.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
</li>
</ul>
<h1 id="五、Quartz-的体系结构"><a href="#五、Quartz-的体系结构" class="headerlink" title="五、Quartz 的体系结构"></a>五、Quartz 的体系结构</h1><hr>
<p><img src="https://img-blog.csdnimg.cn/4769f8666beb4f67ab6929fb5f54a569.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h1 id="六、Quartz-的几个常用-API"><a href="#六、Quartz-的几个常用-API" class="headerlink" title="六、Quartz 的几个常用 API"></a>六、Quartz 的几个常用 API</h1><hr>
<p>以下是 Quartz 编程 API 的几个重要接口，也是 Quartz 的重要组件：</p>
<ul>
<li>Scheduler 是用于与调度程序交互的主程序接口</li>
</ul>
<p>Scheduler 调度程序，任务执行计划表，只有安排进执行计划的任务 job （通过 scheduler.schedulerJob方法安排进执行计划），当它预先定义的执行时间到了的时候（任务触发 Trigger），该任务才会执行。</p>
<ul>
<li><p>Job 我们预先定义的希望在未来时间能被调度程序执行的时候，可以自定义。</p>
</li>
<li><p>JobDetail ，使用 jobDetail来定义定时任务的实例通过 ，JobBuilder类创建</p>
</li>
<li><p>JobDataMap，可以包含不限量（序列化的）数据对象，在 job 实例执行的时候，可以使用其中的数据； JobDataMap 是 Java Map接口的一个实现，额外增加了一些便于存取基本类型数据的方法。 </p>
</li>
<li><p>Trigger 触发器，Trigger对象是用来执行Job的。当调度一个 Job 时，我们实例一个触发器会调整它的属性来满足 job 执行的条件。表明任务在什么时候会执行，定义了一个已被安排的任务将会在什么时候执行的条件，比如每2秒执行一次</p>
</li>
<li><p>JobBuilder 用于声明一个任务实例，也可以定义关于该任务的详情，比如任务名、组名等，这个声明的实例将会作为一个实际执行的任务。</p>
</li>
<li><p>TriggerBuilder 触发器创建器，用于创建触发器 trigger 实例</p>
</li>
<li><p>JobListener、TriggerListener、SchedulerLisener 监听器，用于对组件的监听。</p>
</li>
</ul>
<h1 id="七、Quartz-入门案例"><a href="#七、Quartz-入门案例" class="headerlink" title="七、Quartz 入门案例"></a>七、Quartz 入门案例</h1><hr>
<p>项目结构：<br><img src="https://img-blog.csdnimg.cn/af1994814305405e8853a1c9e9ad67b4.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>引入Maven依赖</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;UTF-8&quot;</span>?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">project</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">&quot;http://maven.apache.org/POM/4.0.0&quot;</span></span><br><span class="hljs-tag">         <span class="hljs-attr">xmlns:xsi</span>=<span class="hljs-string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span><br><span class="hljs-tag">         <span class="hljs-attr">xsi:schemaLocation</span>=<span class="hljs-string">&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">modelVersion</span>&gt;</span>4.0.0<span class="hljs-tag">&lt;/<span class="hljs-name">modelVersion</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>groupId<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>quartzDemo<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>1.0-SNAPSHOT<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">properties</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">maven.compiler.source</span>&gt;</span>8<span class="hljs-tag">&lt;/<span class="hljs-name">maven.compiler.source</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">maven.compiler.target</span>&gt;</span>8<span class="hljs-tag">&lt;/<span class="hljs-name">maven.compiler.target</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">properties</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependencies</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.quartz-scheduler<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>quartz<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>2.3.2<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.quartz-scheduler<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>quartz-jobs<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>2.3.2<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.projectlombok<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>lombok<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>1.18.24<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">scope</span>&gt;</span>compile<span class="hljs-tag">&lt;/<span class="hljs-name">scope</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.apache.commons<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>commons-lang3<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>3.12.0<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependencies</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">project</span>&gt;</span><br></code></pre></td></tr></table></figure>
<p>自定义Job</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> cn.uni.job;<br><br><span class="hljs-keyword">import</span> org.apache.commons.lang3.time.DateFormatUtils;<br><span class="hljs-keyword">import</span> org.quartz.Job;<br><span class="hljs-keyword">import</span> org.quartz.JobExecutionContext;<br><span class="hljs-keyword">import</span> org.quartz.JobExecutionException;<br><br><span class="hljs-keyword">import</span> java.util.Date;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 自定义 Quartz 框架里的任务</span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyJob</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">Job</span> &#123;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">execute</span><span class="hljs-params">(JobExecutionContext jobExecutionContext)</span> <span class="hljs-keyword">throws</span> JobExecutionException &#123;<br>        <span class="hljs-comment">// 获取当前时间</span><br>        <span class="hljs-type">String</span> <span class="hljs-variable">t</span> <span class="hljs-operator">=</span> DateFormatUtils.format(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>(), <span class="hljs-string">&quot;yyyy-MM-dd HH:mm:ss&quot;</span>);<br>        <span class="hljs-comment">// 工作内容</span><br>        System.out.printf(<span class="hljs-string">&quot;[%s] 工作中...\n&quot;</span>, t);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<p>定义 Scheduler、Trigger与测试</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> cn.uni.main;<br><br><span class="hljs-keyword">import</span> cn.uni.job.MyJob;<br><span class="hljs-keyword">import</span> org.quartz.*;<br><span class="hljs-keyword">import</span> org.quartz.impl.StdSchedulerFactory;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">HelloSchedulerDemo</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>        <span class="hljs-keyword">try</span> &#123;<br>            <span class="hljs-comment">// 1. 调度器 ( Scheduler ) 从工厂中获取调度的实例 (默认: 实例化 new StdSchedulerFactory())</span><br>            <span class="hljs-type">Scheduler</span> <span class="hljs-variable">scheduler</span> <span class="hljs-operator">=</span> StdSchedulerFactory.getDefaultScheduler();<br>            <span class="hljs-comment">// 2. 任务实例 ( JobDetail )</span><br>            <span class="hljs-type">JobDetail</span> <span class="hljs-variable">jobDetail</span> <span class="hljs-operator">=</span> JobBuilder.newJob(MyJob.class) <span class="hljs-comment">// 加载任务类, 与自定义的Job类进行绑定</span><br>                    <span class="hljs-comment">// 参数1: 任务的名称 （唯一实例）, 参数2：任务组名称</span><br>                    .withIdentity(<span class="hljs-string">&quot;job1&quot;</span>, <span class="hljs-string">&quot;jobGroup1&quot;</span>)<br>                    .build();<br>            <span class="hljs-comment">// 3. 触发器 (Trigger)</span><br>            <span class="hljs-type">SimpleTrigger</span> <span class="hljs-variable">trigger</span> <span class="hljs-operator">=</span> TriggerBuilder.newTrigger()<br>                    <span class="hljs-comment">// 参数1: 触发器的名称（唯一实例），参数2：触发器组的名称</span><br>                    .withIdentity(<span class="hljs-string">&quot;trigger1&quot;</span>, <span class="hljs-string">&quot;triggerGroup1&quot;</span>)<br>                    <span class="hljs-comment">// 设置每过 3 秒执行一次</span><br>                    .withSchedule(SimpleScheduleBuilder<br>                            .simpleSchedule()<br>                            .withIntervalInSeconds(<span class="hljs-number">3</span>)<br>                            .repeatForever())<br>                    <span class="hljs-comment">// 设置立马启动</span><br>                    .startNow()<br>                    .build();<br>            <span class="hljs-comment">// 让调度器关联任务和触发器, 保证按照触发器定义的条件执行任务</span><br>            scheduler.scheduleJob(jobDetail, trigger);<br>            <span class="hljs-comment">// 启动调度器</span><br>            scheduler.start();<br>            <span class="hljs-comment">// 停止调度器</span><br>            <span class="hljs-comment">// scheduler.shutdown();</span><br><br>        &#125; <span class="hljs-keyword">catch</span> (SchedulerException e) &#123;<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RuntimeException</span>(e);<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>运行结果（每隔三秒执行一次自定义的Job）：</p>
<figure class="highlight prolog"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs prolog">[<span class="hljs-number">2022</span><span class="hljs-number">-07</span><span class="hljs-number">-08</span> <span class="hljs-number">11</span>:<span class="hljs-number">23</span>:<span class="hljs-number">09</span>] 工作中...<br>[<span class="hljs-number">2022</span><span class="hljs-number">-07</span><span class="hljs-number">-08</span> <span class="hljs-number">11</span>:<span class="hljs-number">23</span>:<span class="hljs-number">12</span>] 工作中...<br>[<span class="hljs-number">2022</span><span class="hljs-number">-07</span><span class="hljs-number">-08</span> <span class="hljs-number">11</span>:<span class="hljs-number">23</span>:<span class="hljs-number">15</span>] 工作中...<br></code></pre></td></tr></table></figure>

<h1 id="八、Quartz组件详情"><a href="#八、Quartz组件详情" class="headerlink" title="八、Quartz组件详情"></a>八、Quartz组件详情</h1><hr>
<h2 id="8-1-Job"><a href="#8-1-Job" class="headerlink" title="8.1 Job"></a>8.1 Job</h2><p>Job：工作任务调度的接口，任务类需要实现该接口。该接口中定义 execute()方法，类似JDK提供的 TimeTask 类的run方法，在里面编写任务执行的业务逻辑</p>
<p>Job实例在Quartz中的生命周期：每次调度器执行Job时，它在调用execute方法前会创建一个新的Job实例，当调用完成后，关联的Job对象实例会被释放，释放的实例将会被垃圾回收机制回收。</p>
<p>比如在入门案例七的基础上修改Job类如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> cn.uni.job;<br><br><span class="hljs-keyword">import</span> org.apache.commons.lang3.time.DateFormatUtils;<br><span class="hljs-keyword">import</span> org.quartz.Job;<br><span class="hljs-keyword">import</span> org.quartz.JobExecutionContext;<br><span class="hljs-keyword">import</span> org.quartz.JobExecutionException;<br><br><span class="hljs-keyword">import</span> java.util.Date;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 自定义 Quartz 框架里的任务</span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyJob</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">Job</span> &#123;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">MyJob</span><span class="hljs-params">()</span>&#123;<br>        System.out.println(<span class="hljs-string">&quot;hello, job!&quot;</span>);<br>    &#125;<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">execute</span><span class="hljs-params">(JobExecutionContext jobExecutionContext)</span> <span class="hljs-keyword">throws</span> JobExecutionException &#123;<br>        <span class="hljs-comment">// 获取当前时间</span><br>        <span class="hljs-type">String</span> <span class="hljs-variable">t</span> <span class="hljs-operator">=</span> DateFormatUtils.format(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>(), <span class="hljs-string">&quot;yyyy-MM-dd HH:mm:ss&quot;</span>);<br>        <span class="hljs-comment">// 工作内容</span><br>        System.out.printf(<span class="hljs-string">&quot;[%s] 工作中...\n&quot;</span>, t);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<p>运行结果：</p>
<figure class="highlight prolog"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs prolog">hello, job!<br>[<span class="hljs-number">2022</span><span class="hljs-number">-07</span><span class="hljs-number">-08</span> <span class="hljs-number">12</span>:<span class="hljs-number">34</span>:<span class="hljs-number">38</span>] 工作中...<br>hello, job!<br>[<span class="hljs-number">2022</span><span class="hljs-number">-07</span><span class="hljs-number">-08</span> <span class="hljs-number">12</span>:<span class="hljs-number">34</span>:<span class="hljs-number">41</span>] 工作中...<br></code></pre></td></tr></table></figure>
<p>从这里可以看出，Job类每次执行前都被重新定义过。</p>
<h2 id="8-2-JobDetail"><a href="#8-2-JobDetail" class="headerlink" title="8.2 JobDetail"></a>8.2 JobDetail</h2><p>JobDetail为Job实例提供了许多设置属性，以及 JobDataMap成员变量属性，它用来存储特定 job 实例的状态信息，调度器需要借助 jobDetail 对象来添加 job 实例。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">JobDetail</span> <span class="hljs-variable">jobDetail</span> <span class="hljs-operator">=</span> JobBuilder.newJob(MyJob.class) <span class="hljs-comment">// 加载任务类, 与自定义的Job类进行绑定</span><br>    <span class="hljs-comment">// 参数1: 任务的名称 （唯一实例）, 参数2：任务组名称</span><br>    .withIdentity(<span class="hljs-string">&quot;job1&quot;</span>, <span class="hljs-string">&quot;jobGroup1&quot;</span>)<br>    .build();<br>System.out.println(<span class="hljs-string">&quot;名称: &quot;</span> + jobDetail.getKey().getName());<br><span class="hljs-comment">// 如果没有指定组名, 则默认为: DEFAULT</span><br>System.out.println(<span class="hljs-string">&quot;组的名称: &quot;</span> + jobDetail.getKey().getGroup());<br>System.out.println(<span class="hljs-string">&quot;任务类: &quot;</span> + jobDetail.getJobClass().getName());<br></code></pre></td></tr></table></figure>

<p>运行结果：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs makefile"><span class="hljs-section">名称: job1</span><br><span class="hljs-section">组的名称: jobGroup1</span><br><span class="hljs-section">任务类: cn.uni.job.MyJob</span><br></code></pre></td></tr></table></figure>

<p>JobDetail 重要属性：name、group、jobClass、jobDataMap</p>
<h2 id="8-3-任务的形参-JobExecutionContext"><a href="#8-3-任务的形参-JobExecutionContext" class="headerlink" title="8.3 任务的形参 JobExecutionContext"></a>8.3 任务的形参 JobExecutionContext</h2><ul>
<li>当 Scheduler 调用一个Job，就会将 JobExecutionContext对象传递给 Job的execute() 方法</li>
<li>Job 能通过 JobExecutionContext 对象访问到 Quartz 运行时候的环境以及 Job 本身的详细数据<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Override</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">execute</span><span class="hljs-params">(JobExecutionContext jobExecutionContext)</span> <span class="hljs-keyword">throws</span> JobExecutionException &#123;<br>    <span class="hljs-type">JobDetail</span> <span class="hljs-variable">jobDetail</span> <span class="hljs-operator">=</span> jobExecutionContext.getJobDetail();<br>    <span class="hljs-type">JobKey</span> <span class="hljs-variable">key</span> <span class="hljs-operator">=</span> jobDetail.getKey();<br>    System.out.println(<span class="hljs-string">&quot;工作名称: &quot;</span> + key.getName());<br>    System.out.println(<span class="hljs-string">&quot;工作组: &quot;</span> + key.getGroup());<br>&#125;<br></code></pre></td></tr></table></figure></li>
</ul>
<h2 id="8-4-任务的数据传递-JobDataMap"><a href="#8-4-任务的数据传递-JobDataMap" class="headerlink" title="8.4 任务的数据传递 JobDataMap"></a>8.4 任务的数据传递 JobDataMap</h2><p><strong>1）可通过 Map 获取</strong></p>
<ul>
<li>当进行任务调度时，JobDataMap 存储在 JobExecutionContext 中，非常方便获取</li>
<li>JobDataMap 可以用来装在任何可序列化的数据对象，当 job 实例对象被执行时，这些参数对象都会传递给它</li>
<li>JobDataMap 实现了 JDK 的 Map接口，并且添加了非常方便的方法来存取基本数据类型<br>JobDataMap.java<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">JobDataMap</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">StringKeyDirtyFlagMap</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">Serializable</span> &#123;<br>     ...<br>&#125;<br>StringKeyDirtyFlagMap.java<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">StringKeyDirtyFlagMap</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">DirtyFlagMap</span>&lt;String, Object&gt; &#123;<br>    ...<br>&#125;<br></code></pre></td></tr></table></figure></li>
</ul>
<p>DirtyFlagMap.java</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">DirtyFlagMap</span>&lt;K, V&gt; <span class="hljs-keyword">implements</span> <span class="hljs-title class_">Map</span>&lt;K, V&gt;, Cloneable, Serializable &#123;<br>JobDataMap-&gt; StringKeyDirtyFlagMap -&gt; DirtyFlagMap -&gt; Map<br></code></pre></td></tr></table></figure>

<p>JobDetail 和调度器在定义时，都支持通过usingJobData 方法来传递数据给待执行的Job</p>
<p>通过 JobDetail 或 Trigger传递的JobDataMap可在自定义的Job类里接收</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Override</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">execute</span><span class="hljs-params">(JobExecutionContext context)</span> <span class="hljs-keyword">throws</span> JobExecutionException &#123;<br>    <span class="hljs-type">JobDetail</span> <span class="hljs-variable">jobDetail</span> <span class="hljs-operator">=</span> context.getJobDetail();<br>    <span class="hljs-type">JobKey</span> <span class="hljs-variable">key</span> <span class="hljs-operator">=</span> jobDetail.getKey();<br>    <span class="hljs-type">JobDataMap</span> <span class="hljs-variable">map</span> <span class="hljs-operator">=</span> context.getMergedJobDataMap();<br>    <span class="hljs-keyword">for</span> (Map.Entry&lt;String, Object&gt; entry : map.entrySet()) &#123;<br>        System.out.println(<span class="hljs-string">&quot;key=&quot;</span> + entry.getKey() + <span class="hljs-string">&quot;, value=&quot;</span> + entry.getValue());<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<p>这里发现 JobExecutionContext 对象提供了 getMergedJobDataMap() 方法，也就是获取归并后的JobDataMap数据<br>注意：这里归并后的JobDataMap会对传递的Key做去重操作，如果JobDetail和Trigger 对 job传递了重复的key，那么将会留下最后的Trigger传递的JobDataMap中的key和value。</p>
<p>如果想要获取各自的所有 jobDataMap参数，那么可以通过 JobExecutionContext 对象获取到JobDetail或Trigger。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">JobDataMap</span> <span class="hljs-variable">jobDataMap</span> <span class="hljs-operator">=</span> context.getJobDetail().getJobDataMap();<br><span class="hljs-keyword">for</span> (Map.Entry&lt;String, Object&gt; entry : jobDataMap.entrySet()) &#123;<br>    System.out.println(entry.getKey() + <span class="hljs-string">&quot;,&quot;</span> + entry.getValue());<br>&#125;<br><br><span class="hljs-type">JobDataMap</span> <span class="hljs-variable">triggerDataMap</span> <span class="hljs-operator">=</span> context.getTrigger().getJobDataMap();<br><span class="hljs-keyword">for</span> (Map.Entry&lt;String, Object&gt; entry : triggerDataMap.entrySet()) &#123;<br>    System.out.println(entry.getKey() + <span class="hljs-string">&quot;,&quot;</span> + entry.getValue());<br>&#125;<br></code></pre></td></tr></table></figure>

<p><strong>jobDataMap的简化传递</strong></p>
<p>在之前我们是通过 JobExecutionContext 应用上下文对象去获取 JobDataMap的，除此之外，我们可以在自定义的Job类里定义相应的属性，以及对应的一个set方法（可以用 lombok的@Setter），这样在Job执行时，会自动将 JobDataMap对应的参数的值设置到 Job类中。</p>
<ul>
<li>传递参数<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">JobDetail</span> <span class="hljs-variable">jobDetail</span> <span class="hljs-operator">=</span> JobBuilder.newJob(MyJob.class) <br>    .usingJobData(<span class="hljs-string">&quot;task&quot;</span>, <span class="hljs-string">&quot;任务详情&quot;</span>)<br>    .build();<br>- 获取参数<br></code></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Data</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyJob</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">Job</span> &#123;<br><br>    <span class="hljs-keyword">private</span> String task ;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">MyJob</span><span class="hljs-params">()</span>&#123; &#125;<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">execute</span><span class="hljs-params">(JobExecutionContext context)</span> <span class="hljs-keyword">throws</span> JobExecutionException &#123;<br>        System.out.println(task);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
注：如果这里的Key和Value不匹配，那么就不会执行set方法。</li>
</ul>
<h2 id="8-5-有状态的Job和无状态的Job"><a href="#8-5-有状态的Job和无状态的Job" class="headerlink" title="8.5 有状态的Job和无状态的Job"></a>8.5 有状态的Job和无状态的Job</h2><p>● 无状态的Job<br>  ○ 默认情况下，每次调用时都会创建一个新的 jobDataMap<br>● 有状态的 Job<br>  ○ 多次调用 job，可以获取上次的 jobDataMap，这些状态信息存储在 jobDataMap中<br>@PersistjobDataAfterExecution注解： 保存Job执行后的 JobDataMap</p>
<p>案例：使用 Job 实现对 1 + 2 + 3… + 10 的和</p>
<p>自定义 Job </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> cn.uni.job;<br><br><span class="hljs-keyword">import</span> lombok.Data;<br><span class="hljs-keyword">import</span> lombok.Setter;<br><span class="hljs-keyword">import</span> org.apache.commons.lang3.time.DateFormatUtils;<br><span class="hljs-keyword">import</span> org.quartz.*;<br><br><span class="hljs-keyword">import</span> java.util.Arrays;<br><span class="hljs-keyword">import</span> java.util.Date;<br><span class="hljs-keyword">import</span> java.util.Map;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 自定义 Quartz 框架里的任务</span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">@Setter</span><br><span class="hljs-meta">@PersistJobDataAfterExecution</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyJob</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">Job</span> &#123;<br>    <span class="hljs-keyword">private</span> <span class="hljs-type">int</span> i;<br>    <span class="hljs-keyword">private</span> <span class="hljs-type">int</span> sum;<br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">MyJob</span><span class="hljs-params">()</span>&#123; &#125;<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">execute</span><span class="hljs-params">(JobExecutionContext context)</span> &#123;<br>        <span class="hljs-comment">// 获取当前时间</span><br>        <span class="hljs-type">String</span> <span class="hljs-variable">t</span> <span class="hljs-operator">=</span> DateFormatUtils.format(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>(), <span class="hljs-string">&quot;yyyy-MM-dd HH:mm:ss&quot;</span>);<br>        <span class="hljs-comment">// 工作内容</span><br>        System.out.printf(<span class="hljs-string">&quot;[%s] %d\n&quot;</span>, t, sum += (i++));<br>        <span class="hljs-comment">// 更新 JobDataMap</span><br>        context.getJobDetail().getJobDataMap().put(<span class="hljs-string">&quot;i&quot;</span>, i);<br>        context.getJobDetail().getJobDataMap().put(<span class="hljs-string">&quot;sum&quot;</span>, sum);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<p>传参</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> cn.uni.main;<br><br><span class="hljs-keyword">import</span> cn.uni.job.MyJob;<br><span class="hljs-keyword">import</span> org.quartz.*;<br><span class="hljs-keyword">import</span> org.quartz.impl.StdSchedulerFactory;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">HelloSchedulerDemo</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>        <span class="hljs-keyword">try</span> &#123;<br>            <span class="hljs-comment">// 1. 调度器 ( Scheduler ) 从工厂中获取调度的实例 (默认: 实例化 new StdSchedulerFactory())</span><br>            <span class="hljs-type">Scheduler</span> <span class="hljs-variable">scheduler</span> <span class="hljs-operator">=</span> StdSchedulerFactory.getDefaultScheduler();<br>            <span class="hljs-comment">// 2. 任务实例 ( JobDetail )</span><br>            <span class="hljs-type">JobDetail</span> <span class="hljs-variable">jobDetail</span> <span class="hljs-operator">=</span> JobBuilder.newJob(MyJob.class) <span class="hljs-comment">// 加载任务类, 与自定义的Job类进行绑定</span><br>                    .usingJobData(<span class="hljs-string">&quot;i&quot;</span>, <span class="hljs-number">0</span>)<br>                    .usingJobData(<span class="hljs-string">&quot;sum&quot;</span>, <span class="hljs-number">0</span>)<br>                    .build();<br>            <span class="hljs-comment">// 3. 触发器 (Trigger)</span><br>            <span class="hljs-type">SimpleTrigger</span> <span class="hljs-variable">trigger</span> <span class="hljs-operator">=</span> TriggerBuilder.newTrigger()<br>                    <span class="hljs-comment">// 设置每过 1 秒执行一次, 执行 10 次</span><br>                    .withSchedule(SimpleScheduleBuilder<br>                            .simpleSchedule()<br>                            .withIntervalInSeconds(<span class="hljs-number">1</span>)<br>                            .withRepeatCount(<span class="hljs-number">10</span>))<br>                    <span class="hljs-comment">// 设置立马启动</span><br>                    .startNow()<br>                    .build();<br>            <span class="hljs-comment">// 让调度器关联任务和触发器, 保证按照触发器定义的条件执行任务</span><br>            scheduler.scheduleJob(jobDetail, trigger);<br>            <span class="hljs-comment">// 启动调度器</span><br>            scheduler.start();<br>        &#125; <span class="hljs-keyword">catch</span> (SchedulerException e) &#123;<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RuntimeException</span>(e);<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<p>运行结果：</p>
<figure class="highlight subunit"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs subunit">[2022<span class="hljs-string">-07</span><span class="hljs-string">-08</span> 14:21:07] 0<br>[2022<span class="hljs-string">-07</span><span class="hljs-string">-08</span> 14:21:08] 1<br>[2022<span class="hljs-string">-07</span><span class="hljs-string">-08</span> 14:21:09] 3<br>[2022<span class="hljs-string">-07</span><span class="hljs-string">-08</span> 14:21:10] 6<br>[2022<span class="hljs-string">-07</span><span class="hljs-string">-08</span> 14:21:11] 10<br>...后面都差不多，一共执行了10次<br></code></pre></td></tr></table></figure>
<h2 id="8-5-Trigger"><a href="#8-5-Trigger" class="headerlink" title="8.5 Trigger"></a>8.5 Trigger</h2><p>Quartz 有一些不同的触发器类型，用的最多的是 SimpleTriggerImpl 和 CronTriggerImpl<br>1） JobKey<br>表示 Job 实例的表示，触发器被触发时，JobKey指定的 Job 实例会被执行<br>2）startTime<br>表示触发器的时间表，第一次开始被触发的时间，它的数据类型是 java.util.Date<br>3）endTime<br>指定触发器终止被触发的时间，它的数据类型是 java.util.Date<br>案例：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">SimpleTrigger</span> <span class="hljs-variable">trigger</span> <span class="hljs-operator">=</span> TriggerBuilder.newTrigger()<br>    .withIdentity(<span class="hljs-string">&quot;trigger1&quot;</span>, <span class="hljs-string">&quot;group1&quot;</span>)<br>    ...<br>    .build();<br><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">execute</span><span class="hljs-params">(JobExecutionContext context)</span> &#123;<br>    <span class="hljs-comment">// 获取当前时间</span><br>    <span class="hljs-type">String</span> <span class="hljs-variable">t</span> <span class="hljs-operator">=</span> DateFormatUtils.format(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>(), <span class="hljs-string">&quot;yyyy-MM-dd HH:mm:ss&quot;</span>);<br>    System.out.printf(<span class="hljs-string">&quot;[%s] 任务执行 \n&quot;</span>, t);<br>    <span class="hljs-type">Trigger</span> <span class="hljs-variable">trigger</span> <span class="hljs-operator">=</span> context.getTrigger();<br>    <span class="hljs-comment">// JobKey jobKey = trigger.getJobKey();  // 通过 trigger 可获取到 jobKey</span><br>    <span class="hljs-type">TriggerKey</span> <span class="hljs-variable">triggerKey</span> <span class="hljs-operator">=</span> trigger.getKey();<br>    System.out.println(<span class="hljs-string">&quot;jobKey的名称: &quot;</span> + triggerKey.getName());<br>    System.out.println(<span class="hljs-string">&quot;jobKey的组名: &quot;</span> + triggerKey.getGroup());<br>    System.out.println(<span class="hljs-string">&quot;任务开始时间: &quot;</span> + trigger.getStartTime());<br>    System.out.println(<span class="hljs-string">&quot;任务结束时间: &quot;</span> + trigger.getEndTime());<br>&#125;<br></code></pre></td></tr></table></figure>
<p>触发器可以自定义控制任务的开始时间、结束时间、执行次数等参数</p>
<p>&#x2F;&#x2F; 3. 触发器 (Trigger)</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">Date</span> <span class="hljs-variable">endDate</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>();<br>endDate.setTime(endDate.getTime() + <span class="hljs-number">10000</span>); <span class="hljs-comment">// 10秒后停止</span><br><span class="hljs-type">Trigger</span> <span class="hljs-variable">trigger</span> <span class="hljs-operator">=</span> TriggerBuilder.newTrigger()<br>    .withIdentity(<span class="hljs-string">&quot;trigger1&quot;</span>, <span class="hljs-string">&quot;group1&quot;</span>)<br>    .endAt(endDate)<br>    <span class="hljs-comment">// 设置立马启动</span><br>    .startNow()<br>    .build();<br></code></pre></td></tr></table></figure>
<p>这里是通过 endAt 来指定任务的结束时间,  这样比较麻烦，而且只能执行一次，除此之外可以使用SchedulerBuilder的实现类来指定任务的时间</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java">.withSchedule(SimpleScheduleBuilder.simpleSchedule().xxx)<br></code></pre></td></tr></table></figure>
<h3 id="8-5-1-SimpleTrigger-触发器-使用案例"><a href="#8-5-1-SimpleTrigger-触发器-使用案例" class="headerlink" title="8.5.1 SimpleTrigger 触发器 使用案例"></a>8.5.1 SimpleTrigger 触发器 使用案例</h3><p>SimpleTrigger 对于设置和使用是最简单的一种 QuartzTrigger<br>适合需要在特定的日期&#x2F;时间启动的场景，且以一个可能的间隔时间重复执行n次的 Job</p>
<p>接下来的案例均以相同的自定义Job类：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> cn.uni.job;<br><br><span class="hljs-keyword">import</span> lombok.Data;<br><span class="hljs-keyword">import</span> lombok.Setter;<br><span class="hljs-keyword">import</span> org.apache.commons.lang3.time.DateFormatUtils;<br><span class="hljs-keyword">import</span> org.quartz.*;<br><br><span class="hljs-keyword">import</span> java.util.Arrays;<br><span class="hljs-keyword">import</span> java.util.Date;<br><span class="hljs-keyword">import</span> java.util.Map;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 自定义 Quartz 框架里的任务</span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">@Setter</span><br><span class="hljs-meta">@PersistJobDataAfterExecution</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyJob</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">Job</span> &#123;<br>    <span class="hljs-keyword">private</span> <span class="hljs-type">int</span> count;<br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">MyJob</span><span class="hljs-params">()</span>&#123; &#125;<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">execute</span><span class="hljs-params">(JobExecutionContext context)</span> &#123;<br>        <span class="hljs-comment">// 获取当前时间</span><br>        <span class="hljs-type">String</span> <span class="hljs-variable">t</span> <span class="hljs-operator">=</span> DateFormatUtils.format(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>(), <span class="hljs-string">&quot;yyyy-MM-dd HH:mm:ss&quot;</span>);<br>        System.out.printf(<span class="hljs-string">&quot;[%s] 任务第 %d 次执行 \n&quot;</span>, t, ++count);<br>        context.getJobDetail().getJobDataMap().put(<span class="hljs-string">&quot;count&quot;</span>, count);<br>    &#125;<br>&#125;<br><br></code></pre></td></tr></table></figure>

<p><strong>例1：设置触发器，让任务在两秒后开始执行</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> cn.uni.main;<br><br><span class="hljs-keyword">import</span> cn.uni.job.MyJob;<br><span class="hljs-keyword">import</span> org.quartz.*;<br><span class="hljs-keyword">import</span> org.quartz.impl.StdSchedulerFactory;<br><br><span class="hljs-keyword">import</span> java.util.Date;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">HelloSchedulerDemo</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>        <span class="hljs-keyword">try</span> &#123;<br>            <span class="hljs-comment">// 1. 调度器 ( Scheduler ) 从工厂中获取调度的实例 (默认: 实例化 new StdSchedulerFactory())</span><br>            <span class="hljs-type">Scheduler</span> <span class="hljs-variable">scheduler</span> <span class="hljs-operator">=</span> StdSchedulerFactory.getDefaultScheduler();<br>            <span class="hljs-comment">// 2. 任务实例 ( JobDetail )</span><br>            <span class="hljs-type">JobDetail</span> <span class="hljs-variable">jobDetail</span> <span class="hljs-operator">=</span> JobBuilder.newJob(MyJob.class) <span class="hljs-comment">// 加载任务类, 与自定义的Job类进行绑定</span><br>                    .usingJobData(<span class="hljs-string">&quot;count&quot;</span>, <span class="hljs-number">0</span>)<br>                    .build();<br>            <span class="hljs-comment">// 3. 触发器 (Trigger)</span><br>            <span class="hljs-type">Date</span> <span class="hljs-variable">startDate</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>();<br>            startDate.setTime(startDate.getTime() + <span class="hljs-number">2000</span>);<br>            <span class="hljs-type">Trigger</span> <span class="hljs-variable">trigger</span> <span class="hljs-operator">=</span> TriggerBuilder.newTrigger()<br>                    <span class="hljs-comment">// 设置 2秒后开始执行, 执行1次</span><br>                    .startAt(startDate)<br>                    .build();<br>            scheduler.scheduleJob(jobDetail, trigger);<br>            <span class="hljs-comment">// 启动调度器</span><br>            scheduler.start();<br>        &#125; <span class="hljs-keyword">catch</span> (SchedulerException e) &#123;<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RuntimeException</span>(e);<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<p>运行结果：</p>
<figure class="highlight subunit"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs subunit">[2022<span class="hljs-string">-07</span><span class="hljs-string">-08</span> 15:07:39] 任务第 1 次执行 <br></code></pre></td></tr></table></figure>
<p><strong>例2：设置触发器，让任务在两秒后开始执行，并且每次间隔一秒重复执行三次</strong><br>（代码其他部分跟上述相同，故忽略，下同）</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">Trigger</span> <span class="hljs-variable">trigger</span> <span class="hljs-operator">=</span> TriggerBuilder.newTrigger()<br>    <span class="hljs-comment">// 设置 2秒后开始执行, 执行1次</span><br>    .startAt(startDate)<br>    .withSchedule(SimpleScheduleBuilder.simpleSchedule()<br>                  .withIntervalInSeconds(<span class="hljs-number">1</span>)   <span class="hljs-comment">// 间隔1秒</span><br>                  .withRepeatCount(<span class="hljs-number">2</span>))    <span class="hljs-comment">// 重复两次</span><br>    .build();<br></code></pre></td></tr></table></figure>
<p>运行结果：</p>
<figure class="highlight subunit"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs subunit">[2022<span class="hljs-string">-07</span><span class="hljs-string">-08</span> 15:13:18] 任务第 1 次执行 <br>[2022<span class="hljs-string">-07</span><span class="hljs-string">-08</span> 15:13:19] 任务第 2 次执行 <br>[2022<span class="hljs-string">-07</span><span class="hljs-string">-08</span> 15:13:20] 任务第 3 次执行 <br></code></pre></td></tr></table></figure>
<p><strong>例3：设置触发器，每3秒执行1次任务，设置重复执行4次，但指定任务在10秒后就结束</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">Date</span> <span class="hljs-variable">startDate</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>();<br><span class="hljs-type">Date</span> <span class="hljs-variable">endDate</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>();<br>startDate.setTime(startDate.getTime() + <span class="hljs-number">2000</span>);<br>endDate.setTime(startDate.getTime() + <span class="hljs-number">10000</span>);<br><span class="hljs-type">Trigger</span> <span class="hljs-variable">trigger</span> <span class="hljs-operator">=</span> TriggerBuilder.newTrigger()<br>    <span class="hljs-comment">// 设置2秒后开始执行</span><br>    .startAt(startDate)<br>    .withSchedule(SimpleScheduleBuilder.simpleSchedule()<br>                  .withIntervalInSeconds(<span class="hljs-number">3</span>)                <span class="hljs-comment">// 间隔3秒</span><br>                  .withRepeatCount(<span class="hljs-number">4</span>))    <span class="hljs-comment">// 重复执行四次</span><br>    .endAt(endDate)<br>    .build();<br></code></pre></td></tr></table></figure>
<p>运行结果：可以发现，尽管设置重复执行四次，但是结束时间在重复执行第四次前就到了，任务就不会继续执行，这里是以任务的结束时间为准。</p>
<figure class="highlight subunit"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs subunit">[2022<span class="hljs-string">-07</span><span class="hljs-string">-08</span> 15:19:32] 任务第 1 次执行 <br>[2022<span class="hljs-string">-07</span><span class="hljs-string">-08</span> 15:19:35] 任务第 2 次执行 <br>[2022<span class="hljs-string">-07</span><span class="hljs-string">-08</span> 15:19:38] 任务第 3 次执行 <br>[2022<span class="hljs-string">-07</span><span class="hljs-string">-08</span> 15:19:41] 任务第 4 次执行 <br></code></pre></td></tr></table></figure>
<p>SimpleTrigger 需要注意的点：<br>● SimpleTrigger的属性有：开始时间、结束时间、重复次数 和 重复的时间间隔<br>● 重复次数属性的值可以为 0 、正整数、或常量 SimpleTrigger.REPEAT_INDEFINITELY<br>● 重复的时间间隔属性值必须为0或长整型的正整数，以毫秒作为时间单位，当重复的时间间隔为 0 时，意味着与Trigger同时触发执行。<br>● 如果有执行结束时间属性值，刚结束时间属性优先于重复次数属性，这样的好处在于：当我们需要创建每一个间隔10秒钟触发一次直到指定的结束时间的Trigger，而无需去计算从开始到结束的所重复的次数，只需要简单的指定结束时间，和 SimpleTrigger.REPEAT_INDEFINITELY 作为重复次数的属性值即可。<br>● 其中的 withRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY)) 可以用 .repeatForever() 代替，效果都是一样的，指定任务一直执行</p>
<h3 id="8-5-2-CronTrigger-触发器"><a href="#8-5-2-CronTrigger-触发器" class="headerlink" title="8.5.2 CronTrigger 触发器"></a>8.5.2 CronTrigger 触发器</h3><p>适合类似日历那样来按日程触发任务，而不是像 SimpleTrigger 那样每隔特定的间隔时间触发，CronTriggers 通常比 SimpleTrigger 更有效果，因为它是基于日历的作业调度器。</p>
<p>使用 CtronTrigger ，可以指定诸如：”每个周五的中午12点”，或者 “每个工作日的 9:00” 或者 “ 从每个周一、周三、周五的上午 9:00 到 上午 10 : 00 之间每隔五分钟” 这样日程安排来触发。</p>
<p>CronTrigger 可以和 SimpleTrigger一样，startTime指定日程开始时间，也有可选的endTime指定日程结束时间。</p>
<p>Cron Expressions —— Cron 表达式</p>
<p>可以直接利用工具，在线生成  <a target="_blank" rel="noopener" href="https://tooltt.com/quartzcron/">在线QuartzCron定时任务表达式在线生成工具 - ToolTT在线工具箱</a></p>
<h1 id="九、Quartz-任务调度"><a href="#九、Quartz-任务调度" class="headerlink" title="九、Quartz 任务调度"></a>九、Quartz 任务调度</h1><hr>
<h2 id="9-1-配置、资源-SchedulerFactory"><a href="#9-1-配置、资源-SchedulerFactory" class="headerlink" title="9.1 配置、资源 SchedulerFactory"></a>9.1 配置、资源 SchedulerFactory</h2><p>Quartz 以模块方式构建，因此在运行时几个组件必须配合使用。所有的 Scheduler 实例由 SchedulerFactory 创建<br>Quartz 的三个核心概念：调度器、任务和触发器，三者之间的关系是：<br><img src="https://img-blog.csdnimg.cn/873b003339264042a444dea87d895414.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>Job 三大要素：Scheduler、JobDetail、Trigger，而Trigger对于job而言就好比一个驱动器；<br>没有触发器来定时驱动作业，作业就无法运行；<br>对于 Job 而言，一个Job可以对应多个Trigger，但对于Trigger而言，一个Trigger只能对应一个Job。<br>所以一个Trigger 只能被指派给一个Job；如果需要一个更复杂的触发计划，就可以创建多个 Trigger 并指派它们给同一个 Job</p>
<h2 id="9-2-Scheduler的两种创建方式"><a href="#9-2-Scheduler的两种创建方式" class="headerlink" title="9.2 Scheduler的两种创建方式"></a>9.2 Scheduler的两种创建方式</h2><h3 id="9-2-1-StdSchedulerFacotry"><a href="#9-2-1-StdSchedulerFacotry" class="headerlink" title="9.2.1 StdSchedulerFacotry"></a>9.2.1 StdSchedulerFacotry</h3><p>Quartz 默认的 SchedulerFactory有以下两个特点</p>
<ul>
<li>使用一组参数（java.util.Properties）来创建和初始化 Quartz 调度器</li>
<li>配置参数一般存储在 quartz.properties 文件中</li>
</ul>
<p>调用该调度器的位置在 org.quartz.impl下的 StdSchedulerFactory.java 类中的 initialize() 方法<br>● 调用 getSchedular 方法就能创建和初始化调度器对象</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">SchedulerFactory</span> <span class="hljs-variable">schedulerFactory</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">StdSchedulerFactory</span>();<br><span class="hljs-type">Scheduler</span> <span class="hljs-variable">scheduler</span> <span class="hljs-operator">=</span> schedulerFactory.getScheduler();<br></code></pre></td></tr></table></figure>
<p>等同于</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">Scheduler</span> <span class="hljs-variable">scheduler</span> <span class="hljs-operator">=</span> StdSchedulerFactory.getDefaultScheduler();<br></code></pre></td></tr></table></figure>

<p><strong>用法一：输出调度器开始的时间（重要：使得任务和触发器进行关联）</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs java">Date <span class="hljs-title function_">scheduleJob</span><span class="hljs-params">(JobDetail jobDetail, Trigger trigger)</span><br><span class="hljs-comment">// 绑定 job 和 trigger</span><br><span class="hljs-type">Date</span> <span class="hljs-variable">date</span> <span class="hljs-operator">=</span> scheduler.scheduleJob(jobDetail, trigger);<br>System.out.println(<span class="hljs-string">&quot;Scheduler&#x27;s start time is &quot;</span> + DateFormatUtils.format(date, <span class="hljs-string">&quot;yyyy-MM-dd HH:mm:ss&quot;</span>));<br></code></pre></td></tr></table></figure>

<p><strong>用法二：启动任务调度</strong><br>void start();</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java">scheduler.start();<br></code></pre></td></tr></table></figure>

<p><strong>用法三：任务调度器挂起，即暂停操作</strong><br>void standby();</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java">scheduler.standby();<br></code></pre></td></tr></table></figure>

<p><strong>用法四：关闭任务调度</strong><br>void shutdown();</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs java">shutdown(<span class="hljs-literal">true</span>) ：表示等待所有正在执行的 job 执行完毕之后，再关闭 Scheduler<br><span class="hljs-title function_">shutdown</span><span class="hljs-params">(<span class="hljs-literal">false</span>)</span>：表示直接关闭 Scheduler<br></code></pre></td></tr></table></figure>
<h3 id="9-2-2-DirectSchedulerFacotry-了解）"><a href="#9-2-2-DirectSchedulerFacotry-了解）" class="headerlink" title="9.2.2 DirectSchedulerFacotry(了解）"></a>9.2.2 DirectSchedulerFacotry(了解）</h3><p>DirectSchedulerFacotry 是对 SchedulerFactory的直接实现，通过它可直接构建 Scheduler、Threadpool等</p>
<h2 id="9-3-Quartz-配置文件详解"><a href="#9-3-Quartz-配置文件详解" class="headerlink" title="9.3 Quartz 配置文件详解"></a>9.3 Quartz 配置文件详解</h2><p>默认路径： quartz-2.3.2.jar包下的  org.quartz目录中，文件名为 quartz.properties<br>使用时候可在项目的资源目录 resources 中添加 quartz.properties配置文件，去覆盖底层默认的配置文件</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs properties"><span class="hljs-attr">org.quartz.scheduler.instanceName</span>: <span class="hljs-string">DefaultQuartzScheduler</span><br><span class="hljs-attr">org.quartz.scheduler.rmi.export</span>: <span class="hljs-string">false</span><br><span class="hljs-attr">org.quartz.scheduler.rmi.proxy</span>: <span class="hljs-string">false</span><br><span class="hljs-attr">org.quartz.scheduler.wrapJobExecutionInUserTransaction</span>: <span class="hljs-string">false</span><br><br><span class="hljs-attr">org.quartz.threadPool.class</span>: <span class="hljs-string">org.quartz.simpl.SimpleThreadPool</span><br><span class="hljs-attr">org.quartz.threadPool.threadCount</span>: <span class="hljs-string">10</span><br><span class="hljs-attr">org.quartz.threadPool.threadPriority</span>: <span class="hljs-string">5</span><br><span class="hljs-attr">org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread</span>: <span class="hljs-string">true</span><br><br><span class="hljs-attr">org.quartz.jobStore.misfireThreshold</span>: <span class="hljs-string">60000</span><br><br><span class="hljs-attr">org.quartz.jobStore.class</span>: <span class="hljs-string">org.quartz.simpl.RAMJobStore</span><br></code></pre></td></tr></table></figure>
<p>配置文件的组成部分：</p>
<p>● 调度器属性<br><code>org.quartz.scheduler.instanceName</code> 属性用来区分特定的调度器实例，可以按照功能用途来给调度器起名。<br><code>org.quartz.scheduler.instanceId</code> 属性和前者一样，也允许任何字符串，但这个值必须在所有调度器实例中是唯一的，尤其是在一个集群环境中，作为集群的唯一key，假如想让Quartz帮助生成这个值，可以设置为 AUTO。<br>● 线程池属性<br><strong>threadCount</strong><br>处理 Job 的线程个数， 至少为1，最多建议不超过100，否则容易卡顿<br><strong>threadPriority</strong><br>线程的优先级，优先级别高的线程比级别低的线程优先得到执行。最小为1，最大为10，默认为5<br><strong>org.quartz.threadPool.class</strong><br>一个实现了 org.quartz.spi.ThreadPool 接口的类，Quartz 自带的线程池实现类是 org.quartz.smpl.SimpleThreadPool<br>● 作业存储设置<br>描述了再调度器实例的生命周期中， Job 和 Trigger 信息是如何被存储的。<br>● 插件配置<br>满足特定需求用到的Quartz插件的配置<br>Quartz配置的两种方式：① 配置文件 ② Java配置类</p>
<h3 id="9-3-1-自定义-quartz-properties"><a href="#9-3-1-自定义-quartz-properties" class="headerlink" title="9.3.1 自定义 quartz.properties"></a>9.3.1 自定义 quartz.properties</h3><p>这里配置了 threadCount 线程的个数为负，故一启动就报错了，说明我们配置的内容是有用的。<br><img src="https://img-blog.csdnimg.cn/ac3983fbf23f494680d3d13e21fa5d2a.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h3 id="9-3-2-代码配置"><a href="#9-3-2-代码配置" class="headerlink" title="9.3.2 代码配置"></a>9.3.2 代码配置</h3><p>通过 Properties 对象可设置 Quertz框架的参数。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> org.quartz.Scheduler;<br><span class="hljs-keyword">import</span> org.quartz.SchedulerException;<br><span class="hljs-keyword">import</span> org.quartz.impl.StdSchedulerFactory;<br><br><span class="hljs-keyword">import</span> java.util.Properties;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">QuartzConfig</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> <span class="hljs-keyword">throws</span> SchedulerException &#123;<br>        <span class="hljs-comment">// 创建工厂实例</span><br>        <span class="hljs-type">StdSchedulerFactory</span> <span class="hljs-variable">factory</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">StdSchedulerFactory</span>();<br>        <span class="hljs-comment">// 创建配置工厂的属性的对象</span><br>        <span class="hljs-type">Properties</span> <span class="hljs-variable">prop</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Properties</span>();<br>        prop.put(StdSchedulerFactory.AUTO_GENERATE_INSTANCE_ID, <span class="hljs-string">&quot;AUTO&quot;</span>);<br>        <span class="hljs-keyword">try</span>&#123;<br>            <span class="hljs-comment">// 加载属性</span><br>            factory.initialize(prop);<br>            <span class="hljs-type">Scheduler</span> <span class="hljs-variable">scheduler</span> <span class="hljs-operator">=</span> factory.getScheduler();<br>            scheduler.start();<br>        &#125; <span class="hljs-keyword">catch</span> (SchedulerException e) &#123;<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RuntimeException</span>(e);<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<h1 id="十、Quartz-监听器"><a href="#十、Quartz-监听器" class="headerlink" title="十、Quartz 监听器"></a>十、Quartz 监听器</h1><p>QUartz监听器用于当任务调度时，及时获取事件的通知，类似于任务执行过程中的邮件、短信类的提醒。Quartz监听器主要由 <code>JobListener</code>、<code>TriggerListener</code>、<code>SchedulerListener</code> 三种，分别表示任务、触发器和调度器对应的监听器。三者的使用方法类似。</p>
<p>监听器分为 全局监听器 和 非全局监听器，二者的区别在于：</p>
<p>● 全局监听器能够接收到所有的 Job &#x2F; Trigger 的事件通知，<br>● 非全局监听器只能接收到其上注册的 Job 或 Trigger 的事件，不在其上注册的 Job 或 Trigger 则不会进行监听。</p>
<h2 id="10-1-JobListener"><a href="#10-1-JobListener" class="headerlink" title="10.1 JobListener"></a>10.1 JobListener</h2><p>在任务调度过程中，与任务 Job 相关的事件包括： job 开始要执行的提示； job 执行完成的提示。</p>
<p>自定义监听器，需实现 JobListener 接口</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> org.quartz.JobExecutionContext;<br><span class="hljs-keyword">import</span> org.quartz.JobExecutionException;<br><span class="hljs-keyword">import</span> org.quartz.JobListener;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyJobListener</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">JobListener</span> &#123;<br>    <span class="hljs-keyword">public</span> String <span class="hljs-title function_">getName</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-type">String</span> <span class="hljs-variable">name</span> <span class="hljs-operator">=</span> <span class="hljs-built_in">this</span>.getClass().getSimpleName();<br>        System.out.println(<span class="hljs-string">&quot;监听器名称: &quot;</span> + name);<br>        <span class="hljs-keyword">return</span> name;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">jobToBeExecuted</span><span class="hljs-params">(JobExecutionContext context)</span> &#123;<br>        <span class="hljs-type">String</span> <span class="hljs-variable">jobName</span> <span class="hljs-operator">=</span> context.getJobDetail().getKey().getName();<br>        System.out.println(<span class="hljs-string">&quot;job名称是: &quot;</span> + jobName + <span class="hljs-string">&quot;, Scheduler在JobDetail将被执行时调用的方法&quot;</span>);<br>    &#125;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">jobExecutionVetoed</span><span class="hljs-params">(JobExecutionContext context)</span> &#123;<br>        <span class="hljs-type">String</span> <span class="hljs-variable">jobName</span> <span class="hljs-operator">=</span> context.getJobDetail().getKey().getName();<br>        System.out.println(<span class="hljs-string">&quot;job名称是: &quot;</span> + jobName + <span class="hljs-string">&quot;, Scheduler在JobDetail将被执行时, 但又被 TriggerListener 否决时调用的方法&quot;</span>);<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">jobWasExecuted</span><span class="hljs-params">(JobExecutionContext context, JobExecutionException jobException)</span> &#123;<br>        <span class="hljs-type">String</span> <span class="hljs-variable">jobName</span> <span class="hljs-operator">=</span> context.getJobDetail().getKey().getName();<br>        System.out.println(<span class="hljs-string">&quot;job名称是: &quot;</span> + jobName + <span class="hljs-string">&quot;, Scheduler在JobDetail被执行后调用这个方法&quot;</span>);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>● 通过调度器定义全局的Job监听器</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 创建并注册全局的 JobListener</span><br>scheduler.getListenerManager().addJobListener(<br>    <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyJobListener</span>(), EverythingMatcher.allJobs());<br></code></pre></td></tr></table></figure>
<p>● 通过调度器定义局部的Job监听器</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> org.quartz.*;<br><span class="hljs-keyword">import</span> org.quartz.impl.StdSchedulerFactory;<br><span class="hljs-keyword">import</span> org.quartz.impl.matchers.EverythingMatcher;<br><span class="hljs-keyword">import</span> org.quartz.impl.matchers.KeyMatcher;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Demo</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> <span class="hljs-keyword">throws</span> SchedulerException &#123;<br>        <span class="hljs-type">Scheduler</span> <span class="hljs-variable">scheduler</span> <span class="hljs-operator">=</span> StdSchedulerFactory.getDefaultScheduler();<br>        <span class="hljs-type">JobDetail</span> <span class="hljs-variable">jobDetail</span> <span class="hljs-operator">=</span> JobBuilder.newJob(MyJob.class)<br>                .withIdentity(<span class="hljs-string">&quot;job1&quot;</span>, <span class="hljs-string">&quot;jobGroup1&quot;</span>)<br>                .build();<br>        <span class="hljs-type">Trigger</span> <span class="hljs-variable">trigger</span> <span class="hljs-operator">=</span> TriggerBuilder.newTrigger()<br>                .withSchedule(SimpleScheduleBuilder.simpleSchedule()<br>                        .withIntervalInSeconds(<span class="hljs-number">1</span>)<br>                        .repeatForever())<br>                .startNow()<br>                .build();<br>        scheduler.scheduleJob(jobDetail, trigger);<br>        <span class="hljs-comment">// 创建并注册局局的 JobListener</span><br>        scheduler.getListenerManager().addJobListener(<br>                <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyJobListener</span>(),<br>                KeyMatcher.keyEquals(JobKey.jobKey(<span class="hljs-string">&quot;job1&quot;</span>, <span class="hljs-string">&quot;jobGroup1&quot;</span>)));<br><br>        scheduler.start();<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<p>JobListener 接口各方法作用总结：<br>● getName方法，用于获取 JobListener 的名称<br>● jobToBeExecuted方法：Scheduler 在 jobDetail 将要被执行时调用的方法<br>● jobExecutionVetoed方法：Scheduler 在 JobDetail即将被执行，但又被 TriggerListener 否决时会调用的方法<br>● jobWasExecuted方法：Scheduler在JobDetail被执行之后调用的方法</p>
<h2 id="10-2-TriggerListener"><a href="#10-2-TriggerListener" class="headerlink" title="10.2 TriggerListener"></a>10.2 TriggerListener</h2><p>任务调度过程中，与触发器 Trigger 相关的事件包括：触发器出发、触发器未正常触发、触发器完成等。<br>源码：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> org.quartz;<br><br><span class="hljs-keyword">import</span> org.quartz.Trigger.CompletedExecutionInstruction;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">interface</span> <span class="hljs-title class_">TriggerListener</span> &#123;<br><br>    String <span class="hljs-title function_">getName</span><span class="hljs-params">()</span>;<br><br>    <span class="hljs-keyword">void</span> <span class="hljs-title function_">triggerFired</span><span class="hljs-params">(Trigger trigger, JobExecutionContext context)</span>;<br><br>    <span class="hljs-type">boolean</span> <span class="hljs-title function_">vetoJobExecution</span><span class="hljs-params">(Trigger trigger, JobExecutionContext context)</span>;<br>    <br>    <span class="hljs-keyword">void</span> <span class="hljs-title function_">triggerMisfired</span><span class="hljs-params">(Trigger trigger)</span>;<br><br>    <span class="hljs-keyword">void</span> <span class="hljs-title function_">triggerComplete</span><span class="hljs-params">(Trigger trigger, JobExecutionContext context,</span><br><span class="hljs-params">            CompletedExecutionInstruction triggerInstructionCode)</span>;<br>&#125;<br></code></pre></td></tr></table></figure>
<p>其中：<br>● getName方法，用于获取触发器的名称<br>● triggerField方法，当与监听器相关联的 Trigger被触发，Job 上的 execute() 方法被执行时，Scheduler 就调用该方法。<br>● vetoJobExecution 方法，在 Trigger触发后，Job 将要被执行时由 Scheduler 调用这个方法。<br>● triggerMisfired 方法，Scheduler 调用这个方法是在 Trigger 错过触发时。 应该关注此方法中持续事件长的逻辑：在出现许多错过触发的 Trigger 时，长逻辑会导致骨牌效应，应该保持这个上方法尽量小。<br>● triggerCompelet 方法：Trigger 被触发并且完成了 Job的执行时，Scheduler 调用这个方法。</p>
<p>自定义触发器监听器</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> org.quartz.JobExecutionContext;<br><span class="hljs-keyword">import</span> org.quartz.Trigger;<br><span class="hljs-keyword">import</span> org.quartz.TriggerListener;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyTriggerListener</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">TriggerListener</span> &#123;<br>    <span class="hljs-keyword">public</span> String <span class="hljs-title function_">getName</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.getClass().getSimpleName();<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">triggerFired</span><span class="hljs-params">(Trigger trigger, JobExecutionContext context)</span> &#123;<br>        <span class="hljs-type">String</span> <span class="hljs-variable">name</span> <span class="hljs-operator">=</span> trigger.getKey().getName();<br>        System.out.println(<span class="hljs-string">&quot;触发器: &quot;</span> + name + <span class="hljs-string">&quot; 被触发. &quot;</span>);<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">vetoJobExecution</span><span class="hljs-params">(Trigger trigger, JobExecutionContext context)</span> &#123;<br>        <span class="hljs-type">String</span> <span class="hljs-variable">name</span> <span class="hljs-operator">=</span> trigger.getKey().getName();<br>        System.out.println(name + <span class="hljs-string">&quot;没有被触发&quot;</span>);<br>        <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;   <span class="hljs-comment">// false 表示会执行 Job ， true 表示不会执行 Job</span><br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">triggerMisfired</span><span class="hljs-params">(Trigger trigger)</span> &#123;<br>        <span class="hljs-type">String</span> <span class="hljs-variable">name</span> <span class="hljs-operator">=</span> trigger.getKey().getName();<br>        System.out.println(name + <span class="hljs-string">&quot;错过触发&quot;</span>);<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">triggerComplete</span><span class="hljs-params">(Trigger trigger, JobExecutionContext context, Trigger.CompletedExecutionInstruction triggerInstructionCode)</span> &#123;<br>        <span class="hljs-type">String</span> <span class="hljs-variable">name</span> <span class="hljs-operator">=</span> trigger.getKey().getName();<br>        System.out.println(name + <span class="hljs-string">&quot;完成之后触发&quot;</span>);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<p>定义全局的 Trigger Listener</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs java">scheduler.getListenerManager().addTriggerListener(<br>    <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyTriggerListener</span>(), EverythingMatcher.allTriggers());<br>定义局部的 Trigger Listener<br>scheduler.getListenerManager().addTriggerListener(<br>                <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyTriggerListener</span>(), KeyMatcher.keyEquals(TriggerKey.triggerKey(<span class="hljs-string">&quot;trigger1&quot;</span>, <span class="hljs-string">&quot;group1&quot;</span>)));<br></code></pre></td></tr></table></figure>
<h2 id="10-3-SchedulerListener"><a href="#10-3-SchedulerListener" class="headerlink" title="10.3 SchedulerListener"></a>10.3 SchedulerListener</h2><p>SchedulerListener 会在 Scheduler 的生命周期中关键事件发生时被调用。与 Scheduler 有关的事件包括：增加一个 job &#x2F; trigger ，删除一个 job&#x2F; trigger，scheduler 发生严重错误，关闭 scheduler等。</p>
<p>自定义调度器监听器：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> com.sun.xml.internal.fastinfoset.util.StringIntMap;<br><span class="hljs-keyword">import</span> org.quartz.*;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">MySchedulerListener</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">SchedulerListener</span> &#123;<br>    <span class="hljs-comment">// 部署 JobDetail时调用</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">jobScheduled</span><span class="hljs-params">(Trigger trigger)</span> &#123;<br>        <span class="hljs-type">String</span> <span class="hljs-variable">name</span> <span class="hljs-operator">=</span> trigger.getKey().getName();<br>        System.out.println(name + <span class="hljs-string">&quot; jobDetail完成部署&quot;</span>);<br>    &#125;<br>    <span class="hljs-comment">// 卸载 JobDetail时调用</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">jobUnscheduled</span><span class="hljs-params">(TriggerKey triggerKey)</span> &#123;<br>        <span class="hljs-type">String</span> <span class="hljs-variable">name</span> <span class="hljs-operator">=</span> triggerKey.getName();<br>        System.out.println(name + <span class="hljs-string">&quot; 完成卸载&quot;</span>);<br>    &#125;<br><br>    <span class="hljs-comment">// 当 Trigger不再被触发的时候执行的方法</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">triggerFinalized</span><span class="hljs-params">(Trigger trigger)</span> &#123;<br>        System.out.println(<span class="hljs-string">&quot;触发器被移除&quot;</span>);<br>    &#125;<br>    <span class="hljs-comment">// 当 一个Trigger被暂停时执行的方法，假如是 Trigger组的话，triggerName参数将为 null</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">triggerPaused</span><span class="hljs-params">(TriggerKey triggerKey)</span> &#123;<br>        <span class="hljs-type">String</span> <span class="hljs-variable">name</span> <span class="hljs-operator">=</span> triggerKey.getName();<br>        System.out.println(name + <span class="hljs-string">&quot; 正在被暂停&quot;</span>);<br>    &#125;<br><br>    <span class="hljs-comment">// 当一个Trigger组被暂停时执行的方法，假如是 Trigger组的话，triggerName参数将为 null</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">triggersPaused</span><span class="hljs-params">(String triggerGroup)</span> &#123;<br>        System.out.println(<span class="hljs-string">&quot;触发器组 &quot;</span> + triggerGroup + <span class="hljs-string">&quot; 被暂停&quot;</span>);<br>    &#125;<br>    <span class="hljs-comment">// Trigger 从暂停中恢复</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">triggerResumed</span><span class="hljs-params">(TriggerKey triggerKey)</span> &#123;<br>        <span class="hljs-type">String</span> <span class="hljs-variable">name</span> <span class="hljs-operator">=</span> triggerKey.getName();<br>        System.out.println(name + <span class="hljs-string">&quot; 正在从暂停中恢复&quot;</span>);<br>    &#125;<br><br>    <span class="hljs-comment">// Trigger 组从暂停中恢复</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">triggersResumed</span><span class="hljs-params">(String triggerGroup)</span> &#123;<br>        System.out.println(<span class="hljs-string">&quot;触发器组: &quot;</span> + triggerGroup + <span class="hljs-string">&quot; 正在从暂停中恢复&quot;</span>);<br>    &#125;<br>    <span class="hljs-comment">// 添加任务</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">jobAdded</span><span class="hljs-params">(JobDetail jobDetail)</span> &#123;<br>        System.out.println(<span class="hljs-string">&quot;添加工作任务: &quot;</span> + jobDetail.getKey().getName());<br>    &#125;<br>    <span class="hljs-comment">// 删除任务</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">jobDeleted</span><span class="hljs-params">(JobKey jobKey)</span> &#123;<br>        System.out.println(<span class="hljs-string">&quot;删除工作任务: &quot;</span> + jobKey.getName());<br>    &#125;<br>    <span class="hljs-comment">// 暂停任务</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">jobPaused</span><span class="hljs-params">(JobKey jobKey)</span> &#123;<br>        System.out.println(<span class="hljs-string">&quot;暂停工作任务: &quot;</span> + jobKey.getName());<br>    &#125;<br><br>    <span class="hljs-comment">// 暂停任务组</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">jobsPaused</span><span class="hljs-params">(String jobGroup)</span> &#123;<br>        System.out.println(<span class="hljs-string">&quot;暂停工作任务组: &quot;</span> + jobGroup);<br>    &#125;<br>    <span class="hljs-comment">// 恢复任务</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">jobResumed</span><span class="hljs-params">(JobKey jobKey)</span> &#123;<br>        System.out.println(<span class="hljs-string">&quot;恢复工作任务: &quot;</span> + jobKey.getName());<br>    &#125;<br>    <span class="hljs-comment">// 恢复任务组</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">jobsResumed</span><span class="hljs-params">(String jobGroup)</span> &#123;<br>        System.out.println(<span class="hljs-string">&quot;工作组 &quot;</span> + jobGroup + <span class="hljs-string">&quot; 恢复了&quot;</span>);<br>    &#125;<br><br>    <span class="hljs-comment">// scheduler 发生报错时</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">schedulerError</span><span class="hljs-params">(String msg, SchedulerException cause)</span> &#123;<br>        System.out.println(<span class="hljs-string">&quot;调度器报错: &quot;</span> + msg + cause.getUnderlyingException());<br>    &#125;<br><br>    <span class="hljs-comment">// scheduler 被挂起时</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">schedulerInStandbyMode</span><span class="hljs-params">()</span> &#123;<br>        System.out.println(<span class="hljs-string">&quot;调度器被挂起&quot;</span>);<br>    &#125;<br>    <span class="hljs-comment">// scheduler 被开启时</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">schedulerStarted</span><span class="hljs-params">()</span> &#123;<br>        System.out.println(<span class="hljs-string">&quot;调度器开启&quot;</span>);<br>    &#125;<br><br>    <span class="hljs-comment">// scheduler 正在开启</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">schedulerStarting</span><span class="hljs-params">()</span> &#123;<br>        System.out.println(<span class="hljs-string">&quot;调度器正在开启&quot;</span>);<br>    &#125;<br><br>    <span class="hljs-comment">// scheduler 关闭</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">schedulerShutdown</span><span class="hljs-params">()</span> &#123;<br>        System.out.println(<span class="hljs-string">&quot;调度器被关闭&quot;</span>);<br>    &#125;<br><br>    <span class="hljs-comment">// scheduler 正在被关闭</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">schedulerShuttingdown</span><span class="hljs-params">()</span> &#123;<br>        System.out.println(<span class="hljs-string">&quot;调度器正在被关闭&quot;</span>);<br>    &#125;<br><br>    <span class="hljs-comment">// scheduler 数据被清除</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">schedulingDataCleared</span><span class="hljs-params">()</span> &#123;<br>        System.out.println(<span class="hljs-string">&quot;调度器数据被清除的时候调用&quot;</span>);<br>    &#125;<br>&#125;<br><br><br><span class="hljs-comment">// 添加监听器</span><br>scheduler.getListenerManager().addSchedulerListener(<br>    <span class="hljs-keyword">new</span> <span class="hljs-title class_">MySchedulerListener</span>()<br>);<br><br><span class="hljs-comment">// 移除监听器</span><br>scheduler.getListenerManager().removeSchedulerListener(<br>    <span class="hljs-keyword">new</span> <span class="hljs-title class_">MySchedulerListener</span>()<br>);<br></code></pre></td></tr></table></figure>


                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/%E4%BB%BB%E5%8A%A1%E8%B0%83%E5%BA%A6/" class="category-chain-item">任务调度</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/Quartz/">#Quartz</a>
      
        <a href="/tags/%E4%BB%BB%E5%8A%A1%E8%B0%83%E5%BA%A6/">#任务调度</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>Quartz 笔记1 任务调度框架 _ 核心组件 Job、Trigger 和 Scheduler _ 入门案例 _ 监听器</div>
      <div>http://example.com/2022/07/09/Quartz 笔记1 任务调度框架 _ 核心组件 Job、Trigger 和 Scheduler _ 入门案例 _ 监听器/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>John Doe</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年7月9日</div>
        </div>
      
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2022/07/10/Quartz%20%E7%AC%94%E8%AE%B02%20%E4%BB%BB%E5%8A%A1%E8%B0%83%E5%BA%A6%E6%A1%86%E6%9E%B6%20_%20%E4%BB%BB%E5%8A%A1%E6%8C%81%E4%B9%85%E5%8C%96%20_%20SpringBoot%20%E6%95%B4%E5%90%88%20Quartz%20%E5%85%A5%E9%97%A8%E6%A1%88%E4%BE%8B%20_%20%E9%80%9A%E8%BF%87web%E6%8E%A7%E5%88%B6%E8%B0%83%E5%BA%A6%E5%99%A8%E7%9A%84%E5%90%AF%E5%8A%A8%E3%80%81%E6%9A%82%E5%81%9C%E3%80%81%E7%BB%A7%E7%BB%AD%E5%92%8C%E5%81%9C%E6%AD%A2/" title="Quartz 笔记2 任务调度框架 _ 任务持久化 _ SpringBoot 整合 Quartz 入门案例 _ 通过web控制调度器的启动、暂停、继续和停止">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Quartz 笔记2 任务调度框架 _ 任务持久化 _ SpringBoot 整合 Quartz 入门案例 _ 通过web控制调度器的启动、暂停、继续和停止</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/07/06/Java%E5%AE%9E%E7%8E%B0%E5%B0%86JSON%E6%96%87%E4%BB%B6%E5%AF%BC%E5%87%BA%E5%88%B0Excel/" title="Java实现将JSON文件导出到Excel">
                        <span class="hidden-mobile">Java实现将JSON文件导出到Excel</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
